import request from '@/utils/request'
import { API_CONFIG } from '@/api/config'

// 查询当前用户的订单列表
export function listUserOrder(query) {
  const params = {
    ...query,
    orgId: API_CONFIG.ORG_ID
  }
  return request({
    url: '/app/order/list',
    method: 'get',
    params: params
  })
}

// 获取当前用户的订单详细信息
export function getUserOrder(orderId, userId) {
  return request({
    url: '/app/order/' + orderId,
    method: 'get',
    params: {
      orgId: API_CONFIG.ORG_ID,
      userId: userId
    }
  })
}

// 根据订单ID获取订单商品列表
export function getOrderItems(orderId, userId) {
  return request({
    url: '/app/order/items/' + orderId,
    method: 'get',
    params: {
      orgId: API_CONFIG.ORG_ID,
      userId: userId
    }
  })
}

// 根据订单号获取订单商品列表
export function getOrderItemsByNo(orderNo) {
  return request({
    url: '/app/order/items/by-no/' + orderNo,
    method: 'get',
    params: {
      orgId: API_CONFIG.ORG_ID
    }
  })
}

// 根据订单号查询订单详情（包含商品信息）
export function getOrderDetailByNo(orderNo) {
  return request({
    url: '/app/order/list-with-products',
    method: 'get',
    params: {
      orderNo: orderNo,
      orgId: API_CONFIG.ORG_ID
    }
  })
}

// 查询当前用户的订单列表（包含商品详情）
export function listUserOrderWithProducts(query, userId) {
  const params = {
    ...query,
    orgId: API_CONFIG.ORG_ID,
    userId: userId
  }
  return request({
    url: '/app/order/list-with-products',
    method: 'get',
    params: params
  })
}

// 获取订单详情（包含商品信息）
export function getOrderDetail(orderId, userId) {
  return request({
    url: '/app/order/list-with-products',
    method: 'get',
    params: {
      orderId: orderId,
      orgId: API_CONFIG.ORG_ID,
      userId: userId
    }
  })
}

// 更新订单状态
export function updateOrderStatus(orderId, status) {
  return request({
    url: `/app/order/updateStatus/${orderId}/${status}`,
    method: 'post',
    params: {
      orgId: API_CONFIG.ORG_ID
    }
  })
}

// 申请退款
export function applyRefund(refundData) {
  const requestData = {
    ...refundData,
    orgId: API_CONFIG.ORG_ID
  }
  return request({
    url: '/app/order/refund/apply',
    method: 'post',
    data: requestData
  })
}

// 微信支付创建预支付订单
export function createWechatPayOrder(orderData) {
  // API层：创建预支付订单（移除敏感日志输出）
  
  const requestData = {
    ...orderData,
    orgId: API_CONFIG.ORG_ID
  }
  
  return request({
    url: '/wxpay/create',
    method: 'post',
    data: requestData
  })
}

// 查询微信支付订单状态
export function queryWechatPayOrder(queryData) {
  const requestData = {
    ...queryData,
    orgId: API_CONFIG.ORG_ID
  }
  return request({
    url: '/wxpay/queryOrder',
    method: 'post',
    data: requestData
  })
}

// 获取微信商户公开信息（如 merchant_id），用于前端组件（weappOrderConfirm）
export function fetchWxMerchantPublicInfo(params) {
  const query = {
    orgId: API_CONFIG.ORG_ID,
    wechatAppId: API_CONFIG.WECHAT_APPID,
    ...params
  }
  return request({
    url: '/wxpay/merchant/publicInfo',
    method: 'get',
    params: query
  })
}

// 查询订单退款状态
export function getRefundStatus(orderNo) {
  return request({
    url: '/app/order/refund/status/' + orderNo,
    method: 'get',
    params: {
      orgId: API_CONFIG.ORG_ID
    }
  })
}

// 提交商品评价
export function submitComment(commentData) {
  const requestData = {
    ...commentData,
    orgId: API_CONFIG.ORG_ID
  }
  return request({
    url: '/app/order/comment/submit',
    method: 'post',
    data: requestData
  })
}

// 检查订单商品是否已评价
export function checkCommentStatus(orderId, productId) {
  return request({
    url: `/app/order/comment/check/${orderId}/${productId}`,
    method: 'get',
    params: {
      orgId: API_CONFIG.ORG_ID
    }
  })
}

// 上传评价图片
export function uploadCommentImage(file) {
  const formData = new FormData()
  formData.append('file', file)
  return request({
    url: '/app/order/comment/upload',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 创建订单
export function createOrder(orderData) {
  const requestData = {
    ...orderData,
    orgId: API_CONFIG.ORG_ID
  }
  return request({
    url: '/app/order/create',
    method: 'post',
    data: requestData
  })
}

// 更新订单收货地址
export function updateOrderAddress(orderId, addressId, userId) {
  return request({
    url: '/app/order/updateAddress',
    method: 'post',
    data: {
      orderId: orderId,
      addressId: addressId,
      userId: userId,
      orgId: API_CONFIG.ORG_ID
    }
  })
}

// 获取订单统计信息
export function getOrderStats(userId) {
  const params = {
    orgId: API_CONFIG.ORG_ID
  }
  
  // 如果传递了userId参数，则添加到请求参数中
  if (userId) {
    params.userId = userId
  }
  
  return request({
    url: '/app/order/stats',
    method: 'get',
    params: params
  })
}

// 应用优惠券到订单
export function applyCouponToOrder(orderId, userId, discountAmount) {
  return request({
    url: '/app/order/applyCoupon',
    method: 'post',
    data: {
      orderId: orderId,
      userId: userId,
      orgId: API_CONFIG.ORG_ID,
      discountAmount: discountAmount
    }
  })
}

// 根据订单号查询物流信息
export function getLogisticsByOrderNo(orderNo, userId) {
  return request({
    url: '/app/order/logistics/' + orderNo,
    method: 'get',
    params: {
      userId: userId,
      orgId: API_CONFIG.ORG_ID
    }
  })
}

// 查询订单发货状态
export function queryShippingStatus(data) {
  return request({
    url: '/fun/logistics/queryShippingStatus',
    method: 'post',
    data
  })
}

// 获取物流跟踪信息（调用外部API）
export function getLogisticsTracking(orderNo) {
  return request({
    url: '/app/logistics/tracking/' + orderNo,
    method: 'get'
  })
}
